package com.aistar.user.controller;



import com.aistar.common.util.IdWorker;
import com.aistar.common.vo.Result;
import com.aistar.common.vo.StatusCode;
import com.aistar.user.entity.User;
import com.aistar.user.service.IUserService;
import com.aistar.common.util.SMSCode;
import com.aistar.user.util.TokenUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by js on 2019/6/24.
 */

@RestController
public class LoginController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private SMSCode smsCode;

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    //登录-get 查询
    @GetMapping("/user")
    public Result login(User user){


        Map<String,Object> map=new HashMap();
        Result result=null;
        User u=iUserService.findUser(user);//查询数据库有没有该用户
        if (null==u){
            result=new Result(false, StatusCode.ERROR,"账号不正确");
        }else{
            //若有该用户,判断密码是否正确
            if (bCryptPasswordEncoder.matches(user.getPassword(),u.getPassword())){
                //登录成功之后 把user信息放到redis中 key: user_id ,value: u
//                redisTemplate.opsForValue().set("user_"+u.getId(),u);

                //登录成功之后产生一个token
                String token=tokenUtil.getToken(u);

                map.put("token",token);
                map.put("user",u);
                result=new Result(true, StatusCode.OK,"查询成功",map);

            }else {
                result=new Result(false, StatusCode.LOGINERROR,"密码错误");
            }
        }
        return result;
    }

    @CrossOrigin
    @GetMapping("/user/{phone}")
    public Result findByPhone(@PathVariable("phone") String phone){


        Result result=null;

        User u =iUserService.findByPhone(phone);

        if (null==u){
            result=new Result(false, StatusCode.ERROR,"没有该用户");
        }else {
            result=new Result(true,StatusCode.OK,"存在该用户",u);
        }
        return result;
    }

    //注册保存一个用户
    @PostMapping("/user")
    public Result save(User user){
        Result result=null;
        user.setId(idWorker.nextId());
        //获得页面传过来的密码
        String password=user.getPassword();
        password=bCryptPasswordEncoder.encode(password);
        user.setPassword(password);
        System.out.println(user);
        iUserService.save(user);
        result=new Result(true,StatusCode.OK,"注册成功",user);
        return result;
    }


    //发送验证码
    @GetMapping("/message/{phone}")
    public Result message(@PathVariable("phone")String phone){
        Result result=null;
        System.out.println(phone);

        //产生一个随机的手机验证码
        String code= RandomStringUtils.randomNumeric(6);

        try {
            smsCode.sendCode(phone,code);
        } catch (Exception e) {
            e.printStackTrace();
        }

        result=new Result(true,StatusCode.OK,"发送成功",code);
        return result;
    }


    @PutMapping("/user")
    public Result forgetPasssword(User user){
        Result result=null;
        String phone=user.getPhone();
        String password=user.getPassword();
        User u=iUserService.findByPhone(phone);
        u.setPassword(bCryptPasswordEncoder.encode(password));
        iUserService.updatePassword(u);

        return new Result(true,StatusCode.OK,"找回密码");

    }
}
